<?php

namespace app\traits;

use app\common\ApiStatus;
use app\common\OsType;
use app\model\SystemAdmin;
use support\Redis;
use support\Response;

/**
 * 后台CURD复用
 * Trait Curd
 */
trait Curd
{

    public function lists(): Response
    {
        list($page, $limit, $where) = $this->params;
        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)
            ->page($page, $limit)
            ->order($this->order_sort)
            ->select();
        $notes = $this->model->notes ?? [];
        //管理员列表
        $adminUserListRedis = Redis::get('admin:list:admin_user');
        if (!$adminUserListRedis) {
            $adminUserList = SystemAdmin::getColumn([], 'username', 'id desc', 'id');
            Redis::set('admin:list:admin_user', json_encode($adminUserList), 'EX', 60 * 60 * 3, 1);
        } else {
            $adminUserList = json_decode($adminUserListRedis, true);
        }
        $notes['admin_user'] = $adminUserList;
        $notes['os_type']    = OsType::values();
        return json(['code' => ApiStatus::API_SUCCESS->value, 'msg' => 'success', 'data' => compact('count', 'list', 'notes')], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
    }

    public function add(): Response
    {
        $errorMessage = '';
        $post         = request()->post();
        if (isset($post['id'])) unset($post['id']);
        if (isset($post['uuid'])) unset($post['uuid']);
        if (isset($post['admin_os_type'])) unset($post['admin_os_type']);

        try {
            $post['create_time']    = time();
            $post['date_v']    = date('Y-m-d');
            $post['admin_uid'] = \request()->admin_uid ?? 0;
            $save              = $this->model->replace()->save($post);
        } catch (\Throwable $e) {
            $errorMessage = $e->getMessage();
            $save         = false;
        }
        return json(['code' => ApiStatus::API_SUCCESS->value, 'msg' => $save ? '操作成功' : '操作失败:' . $errorMessage, 'data' => null], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
    }

    public function edit(): Response
    {
        $errorMessage = '';
        $post         = \request()->post();
        $id           = $post['id'] ?? 0;
        $row          = $this->model->find($id);
        if (empty($row)) return json(['code' => ApiStatus::API_ERROR->value, 'msg' => '操作失败', 'data' => null], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
        if (isset($post['id'])) unset($post['id']);

        try {
            $post['admin_uid'] = request()->admin_uid ?? 0;
            $post['u_time']    = myDate();
            $save              = $row->replace()->save($post);
        } catch (\Throwable $e) {
            $errorMessage = $e->getMessage();
            $save         = false;
        }
        return json(['code' => ApiStatus::API_SUCCESS->value, 'msg' => $save ? '操作成功' : '操作失败:' . $errorMessage, 'data' => null], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
    }

//    public function info(): Response
//    {
//        $post = \request()->post();
//        $id   = $post['id'] ?? 0;
//        $row  = $this->model->find($id);
//
//        return json(['code' => ApiStatus::API_SUCCESS->value, 'msg' => '操作成功', 'data' => $row], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
//    }

    public function delete(): Response
    {
        $errorMessage = '';
        $post         = \request()->post();
        $ids          = $post['ids'] ?? [0];
        if (is_string($ids)) $ids = json_decode($ids, true);
        $row = $this->model->whereIn('id', $ids)->select();
        if ($row->isEmpty()) return json(['code' => ApiStatus::API_ERROR->value, 'msg' => '删除数据不存在', 'data' => null], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
        if (isset($post['id'])) unset($post['id']);
        try {
            $save = $row->delete();
        } catch (\Throwable $e) {
            $errorMessage = $e->getMessage();
            $save         = false;
        }
        return json(['code' => ApiStatus::API_SUCCESS->value, 'msg' => $save ? '操作成功' : '操作失败:' . $errorMessage, 'data' => null], JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
    }

}
